<template>
    <!-- 
    <b-modal ref="my-modal" hide-footer title="Using Component Methods">
     <div class="d-block text-center">
       <h3>Hello From My Modal!</h3>
     </div>
     <b-button class="mt-3" variant="outline-danger" block @click="hideModal">Close Me</b-button>
     <b-button class="mt-2" variant="outline-warning" block @click="toggleModal">Toggle Me</b-button>
   </b-modal> 
    -->
    <!--
        body-class="modalStyle" 表示样式
        size="lg" 表示模态窗口大小 xl,lg,sm,full
        hide-footer 表示隐藏底部按钮
        hide-header 表示隐藏头部内容
        no-close-on-backdrop 表示鼠标点击背景不可关闭
        hide-header-close 表示隐藏头部关闭按钮
        centered 居中
        ok-title=“确定” 
        cancel-title=取消
        @ok="handleOk" 
        @cancel="handleCancel"
        scrollable 滚动条
        hide-backdrop 隐藏背景
        wrapClassName="ant-modal-cust-warp" 
        style="top:5%;height: 85%;overflow-y: hidden" 样式
    -->
    <b-modal ref="my-modal" size="xl" title="新建生产商" scrollable no-close-on-backdrop>
        <div class="d-block text-center">
            <!--begin::div-->
            <div class="m-form m-form--fit m-form--label-align-left m-form--group-seperator-dashed">
                <div class="m-portlet__body">
                    <div class="form-group m-form__group row">
                        <label class="col-md-2 col-form-label">公司名称</label>
                        <div class="col-md-3">
                            <input class="form-control" type="text" maxlength="255" v-model="oemForm.name" placeholder="请输入">
                        </div>
                    </div>
                    <div class="form-group m-form__group row">
                        <label class="col-md-2 col-form-label">简&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;写</label>
                        <div class="col-md-3">
                            <input class="form-control" type="text" maxlength="32" v-model="oemForm.shortName" placeholder="请输入">
                        </div>
                    </div>
                    <div class="form-group m-form__group row">
                        <label class="col-md-2 col-form-label">描&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;述</label>
                        <div class="col-md-6">
                            <textarea class="form-control" maxlength="255" v-model="oemForm.remarks" placeholder="请输入"></textarea>
                        </div>
                    </div>
                    <div class="form-group m-form__group row">
                        <div class="col-md-2">
                            <label class="col-form-label" jEhcLang="xt_province_c_d">省市区县</label>
                        </div>
                        <div class="col-md-3">
                            <input class="form-control" type="hidden" v-model="oemForm.provinceId_">
                            <el-select v-model="oemForm.provinceId">
                                <el-option value=''>请选择</el-option>
                                <el-option 
                                    v-for="item in provinceList"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.id"></el-option>
                            </el-select>
                        </div>
                        <div class="col-md-2">
                            <input class="form-control" type="hidden" v-model="oemForm.cityId_">
                            <el-select v-model="oemForm.cityId">
                                <el-option value=''>请选择</el-option>
                                <el-option 
                                    v-for="item in cityList"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.id"></el-option>
                            </el-select>
                        </div>
                        <div class="col-md-2">
                            <input class="form-control" type="hidden" v-model="oemForm.districtId_">
                            <el-select v-model="oemForm.districtId">
                                <el-option value=''>请选择</el-option>
                                <el-option 
                                    v-for="item in districtList"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.id"></el-option>
                            </el-select>
                        </div>
                        <div class="col-md-2">
                            <input class="form-control" type="text" maxlength="20" v-model="oemForm.address" placeholder="请输入">
                        </div>
                    </div>
                    <div class="form-group m-form__group row">
                        <label class="col-md-2 col-form-label">公司电话</label>
                        <div class="col-md-3">
                            <input class="form-control" type="text" maxlength="255" v-model="oemForm.tel" placeholder="请输入">
                        </div>
                    </div>
                    <div class="form-group m-form__group row">
                        <label class="col-md-2 col-form-label">法&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;人</label>
                        <div class="col-md-3">
                            <input class="form-control" type="text" maxlength="30" v-model="oemForm.legalPerson" placeholder="请输入">
                        </div>
                    </div>
                    <div class="form-group m-form__group row">
                        <label class="col-md-2 col-form-label">公司性质</label>
                        <div class="col-md-3">
                            <input class="form-control" type="text" maxlength="255" v-model="oemForm.type" placeholder="请输入">
                        </div>
                    </div>
                    <div class="form-group m-form__group row">
                        <label class="col-md-2 col-form-label">成立时间</label>
                        <div class="col-md-2">
                            <el-date-picker
                                v-model="oemForm.registrationDate"
                                align="left"
                                style="width:150px"
                                type="date"
                                placeholder="选择日期"
                                value-format="yyyy-MM-dd" format="yyyy-MM-dd"
                                :picker-options="pickerOptions">
                            </el-date-picker>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <template slot="modal-footer">
            <!--自定义按钮-->
            <button type="button" class="btn btn-light-danger font-weight-bold mr-2" block @click="hideModal">
                <i class="icon-xl fa fa-times"></i>取消
            </button> 
            <button type="button" class="btn btn-light-success font-weight-bold mr-2" @click="addOem">
                <i class="icon-xl fas fa-save"></i> 保存
            </button>
        </template>
    </b-modal>        
</template>
<script>
import apiUtil from "@/core/util/apiUtil.js"; 
import { handleNotify,handleAlert,handleConfirm,showWating,closeWating} from "@/core/util/jehcUtil.js";
export default {
    data(){
        return {    
            provinceList:[],
            cityList:[],
            districtList:[],
            provinceId_:"",
            cityId_:"",
            districtId_:"",
            oemForm:{
                name: "",
                shortName:"",
                remarks:"",
                provinceId:"",
                cityId:"",
                districtId:"",
                address:"",
                tel:"",
                legalPerson:"",
                type:"",
                registrationDate:"",
            },
            pickerOptions: {
                // disabledDate(time) {//范围不可选
                //     return time.getTime() > Date.now();
                // },
                shortcuts: [{
                    text: '今天',
                    onClick(picker) {
                        picker.$emit('pick', new Date());
                    }
                }, 
                {
                    text: '昨天',
                    onClick(picker) {
                        const date = new Date();
                        date.setTime(date.getTime() - 3600 * 1000 * 24);
                        picker.$emit('pick', date);
                    }
                }, 
                {
                    text: '一周前',
                    onClick(picker) {
                        const date = new Date();
                        date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                        picker.$emit('pick', date);
                    }
                }]
            }
        }
    },
    components: {
        
    },
    watch:{
        "oemForm.provinceId":{//监听字段变化
            handler:function(newVal,old){                
                if(this.provinceId_ != newVal){
                    this.oemForm.cityId = "";
                    this.oemForm.districtId = ""; 
                    let p_ = this.provinceId_;//原省份值 
                    this.districtList = []; 
                    if(newVal == "" || newVal == undefined || newVal == null){
                    }else{        
                        this.initCityList();                                   
                        if(null != p_ && undefined !== p_ && "" != p_){
                            this.provinceId_ = "";
                        }                
                    }
                }                
            }
        },
        "oemForm.cityId":{//监听字段变化
            handler:function(newVal,old){
                if(this.cityId_ != newVal){
                    this.oemForm.districtId = "";   
                    let c_ = this.cityId_;//原省份值               
                    if(newVal == "" || newVal == undefined || newVal == null){
                    }else{
                        this.initDistrictList();
                        if(null != c_ && undefined !== c_ && "" != c_){
                            this.cityId_ = "";
                        }
                    }
                }               
            }
        },
    },
    mounted() {
        
    },
    methods: {
        addOem(){
            if(this.oemForm.name === ""){
                handleAlert("请输入名称", "warning", 3)
                return;
            }
            // 保存前提示
            this.$confirm("确定保存?", "提示", {type: "warning",}).then(() => {
                apiUtil.post(process.env.VUE_APP_MONITOR_API+"/iotOem/add", this.oemForm).then(({ data }) => {
                    if(data.success){
                        handleAlert("操作成功", "success", 3);
                        this.hideModal();//关闭窗体
                        this.$emit("change",data);//组件传值，即向父级模态框中传递值，采用change方式，目标刷新主列表使用
                    }else {
                        handleAlert("操作失败", "error", 3);
                    }            
                });
            }).catch(()=>{});//注意这里，这里是重点！！！;   
        },
        showModal() {
            this.restForm();
            this.$refs['my-modal'].show();
            this.initProvinceList();
        },
        hideModal() {
            this.$refs['my-modal'].hide()
        },
        toggleModal() {
            // 当模态已隐藏时，我们传递要返回焦点的按钮的ID
            this.$refs['my-modal'].toggle('#toggle-btn')
        },
        restForm(){
            //重置
            Object.assign(this.$data.oemForm, this.$options.data().oemForm);
        },
        initProvinceList(){//读取省份          
            apiUtil.get(process.env.VUE_APP_SYS_API+"/sysCommon/pList").then(({ data }) => {
                this.provinceList = data.data;
            });
        },
        initCityList(){   //读取城市
            let provinceId = this.oemForm.provinceId;     
            this.cityList = [];
            if(null != provinceId && undefined !== provinceId && "" != provinceId){
                apiUtil.get(process.env.VUE_APP_SYS_API+"/sysCommon/cList/"+provinceId).then(({ data }) => {
                    this.cityList = data.data;
                });
            }   
        },
        initDistrictList(){//读取区县
            let cityId = this.oemForm.cityId;  
            this.districtList = [];        
            if(null != cityId && undefined !== cityId && "" != cityId){
                apiUtil.get(process.env.VUE_APP_SYS_API+"/sysCommon/dList/"+cityId).then(({ data }) => {
                    this.districtList = data.data;
                });
            }
        }, 
   }
}
</script>